package eu.news.servlet.admin.system;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.news.data.hibernate.dao.HB_SysConfigDAO;
import eu.news.data.pojo.SysConfig;

/**
 * Map:/admin/system/config/edit/*
 * Function:更新系统配置项
 * Input:前端Ajax传入的数据。
 */
public class EditConfig extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditConfig() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * 更新系统配置项
	 *  Output:
	 *  	200：成功
	 *  	其他:错误信息
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String key = request.getParameter("key");
		String value = request.getParameter("value");
		String remark = request.getParameter("remark");
		response.setCharacterEncoding("UTF-8");
		if(key!=null&&key.trim().length()>0&&value.trim().length()>0){
			HB_SysConfigDAO dao = new HB_SysConfigDAO();
			SysConfig config = dao.getByKey(key);
			//new
			if(config==null){
				config = new SysConfig();
				config.setKey(key);
				config.setValue(value);
				config.setRemark(remark);
				dao.save(config);
			} else {
			//modify
				config.setValue(value);
				config.setRemark(remark);
				dao.update(config);
			}
			dao.commit();
			response.getWriter().print(200);
		}else{
			response.getWriter().print("配置项名称和配置值不能为空");
		}
	}

}
